package com.rayboo.web.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rayboo.model.ClubUser;
import com.rayboo.service.ClubUserService;
import com.rayboo.util.GridPaging;
import com.rayboo.util.JsonTool;
import com.rayboo.vo.Result;

/**
 * 
 * ClubUserController
 * 
 * @author ray
 * @date 2016年9月8日 下午8:03:30
 *
 */
@Controller
@RequestMapping("/users")
public class ClubUserController {

	@Autowired
	ClubUserService clubUserService;

	/**
	 * 获取会员列表信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "", method = { RequestMethod.GET })
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response, GridPaging gridPaging,
			@RequestParam(required = false, defaultValue = "", value = "query") String query) {
		Integer total = clubUserService.count(query);
		List<ClubUser> lst = clubUserService.find(query, gridPaging.genPageable(total));
		ModelAndView mav = new ModelAndView("users/index");
		mav.addObject("lst", lst);
		mav.addObject("query", query);
		mav.addObject("gridPaging", gridPaging);
		return mav;
	}

	/**
	 * 更新用户信息
	 * 
	 * @param request
	 * @param response
	 * @param userid
	 * @param jsonString
	 * @return
	 */
	@RequestMapping(value = "/update/{userid}", method = RequestMethod.POST)
	@ResponseBody
	public Result update(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer userid,
			@RequestBody String jsonString) {
		if (userid == 0 || jsonString == null)
			return Result.genFail("parameter empty");
		ClubUser user = JsonTool.getObject(jsonString, ClubUser.class);
		if (user == null)
			return Result.genFail("user empty");
		// 更新操作
		if (clubUserService.update(userid, user) == null)
			return Result.genFail("更新失败");
		return Result.genSuccess("更新成功");
	}

	/**
	 * 更新用户vip信息
	 * 
	 * @param request
	 * @param response
	 * @param userid
	 * @param jsonString
	 * @return
	 */
	@RequestMapping(value = "/update/{userid}/vip", method = RequestMethod.POST)
	@ResponseBody
	public Result updateVip(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer userid,
			@RequestBody String jsonString) {
		if (userid == 0 || jsonString == null)
			return Result.genFail("parameter empty");
		ClubUser user = JsonTool.getObject(jsonString, ClubUser.class);
		if (user == null)
			return Result.genFail("user empty");
		if (clubUserService.updateVIP(userid, user.getVip()) == null)
			return Result.genFail("更新失败");
		return Result.genSuccess("更新成功");
	}

}
